﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NUS.ISS.ARS.BusinessFacade.Interface;
using NUS.ISS.ARS.BusinessFacade;
using NUS.ISS.ARS.BusinessEntity;
using NUS.ISS.ARS.BusinessEntity.ViewModels;
using System.Web.Helpers;
using System.IO;
using System.Collections;

namespace NUS.ISS.ARS.Web.Admin.Controllers
{
    //[Authorize(Roles="Admin")]

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View(a.GetAllAircraft());
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Aircraft aircraft)
        {
            return View();
        }
    

       


        public ActionResult About()
        {
            IAdminBF a = BFFactory.CreateAdminBFInstance();
            Aircraft a1 = new Aircraft();
            a1.Model = "";
            a.SaveAircraft(a1);
            ViewBag.Message = a1.ErrorMessage;

            return View();
        }


        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
      
        [HttpGet]
        public ActionResult CancelBooking()
        {
            IBookingBF a = BFFactory.CreateBookingBFInstance();
            IEnumerable<FlightBookingTripView> FlightBookingTripView;
            FlightBookingTripView = a.GetAllFlightBookings();
            return View(FlightBookingTripView);
            return View();
        }
        [HttpPost]
        public ActionResult CancelBooking(String reference, String firstName, string lastName)
        {
            IBookingBF a = BFFactory.CreateBookingBFInstance();

            IEnumerable<FlightBookingTripView> FlightBookingTripView;

            if (!((string.IsNullOrEmpty(reference)) && (string.IsNullOrEmpty(firstName)) && (string.IsNullOrEmpty(lastName))))
            {

                FlightBookingTripView = a.GetFlightBookingsByRefNoAndName(reference, firstName, lastName);
            }
            else
            {
                FlightBookingTripView = a.GetAllFlightBookings();

            }
            if (Request.IsAjaxRequest())
            {
                return PartialView(FlightBookingTripView);
            }
            else
            {
                return View(FlightBookingTripView);
            }
            return View();

            //   ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            //  return View(a.GetAllAircraft());

            //   return View(a.GetFlightSchedulesByFlightNumberAirport(flightnumber, fromairportid, toairportid));
        }

        [HttpGet]
        public ActionResult CancelBookingDelete()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CancelBookingDelete(FlightBookingTripView booking)
        {
            return View();
        }



        [HttpGet]
        public ActionResult FlightReport()
        {
            return View();
        }

        [HttpPost]
        public ActionResult FlightReport(string txtFlightNo, string txtFrom, string txtTo)
        {
            try
            {
                IReportBF r = BFFactory.CreateReportBFInstance();
                ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

                //var model = new ItemViewModel()
                //{
                //    NewsList = new List<NewsViewModel>()
                //};

                var model = r.GenerateFlightReport(txtFlightNo, txtFrom, txtTo);

                return PartialView("_SearchFlightReportResults", model);
            }
            catch (Exception e)
            {
                return PartialView("Error");
            }
        }

        [HttpGet]
        public ActionResult RevenueReport()
        {
            return View();
        }

        [HttpPost]
        public ActionResult RevenueReport(string txtFlightNo, string txtFrom, string txtTo)
        {
            try
            {
                ViewBag.txtFlightNo = txtFlightNo;
                ViewBag.txtFrom = txtFrom;
                ViewBag.txtTo = txtTo;

                return PartialView("_SearchRevenueReportResults");
            }
            catch (Exception e)
            {
                return PartialView("Error");
            }
        }

        public ActionResult RenderChart(string txtFlightNo, string txtFrom, string txtTo)
        {
            IReportBF r = BFFactory.CreateReportBFInstance();

            if (String.IsNullOrEmpty(txtFlightNo))
            {
                txtFlightNo = "";
            }
            if (String.IsNullOrEmpty(txtFrom))
            {
                txtFrom = "";
            }
            if (String.IsNullOrEmpty(txtTo))
            {
                txtTo = "";
            }

            var model = r.GenerateRevenueReport(txtFlightNo, txtFrom, txtTo);

            ArrayList xValue = new ArrayList();
            ArrayList yValue = new ArrayList();

            model.ToList().ForEach(rs => xValue.Add(rs.FlightNumber));
            model.ToList().ForEach(rs => yValue.Add(rs.Revenue));

            Chart chart = new Chart(600, 400, theme: ChartTheme.Green);
            chart.AddSeries(chartType: "Bar", 
                            xValue: xValue, 
                            xField: "Revenue ($)",
                            axisLabel: "Revenue",
                            yValues: yValue,
                            yFields: "Flight Number");
            chart.Write(format: "png");

            return null;
        }
    }
}
